package cn.hdufocus.focus_helper.controller;

import cn.hdufocus.focus_helper.bean.ResponseBean;
import cn.hdufocus.focus_helper.entity.User;
import cn.hdufocus.focus_helper.exception.ForbiddenException;
import cn.hdufocus.focus_helper.service.IUserService;
import cn.hdufocus.focus_helper.util.JWTUtil;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    IUserService userService;

    @PostMapping("/login")
    public ResponseBean login(@RequestBody User tempUser) {
        User user = userService.getUser(tempUser.getUsername());
        if (user!=null){
            if (user.getPassword().equals(tempUser.getPassword())) {
                return new ResponseBean(200, "Login success", JWTUtil.sign(tempUser.getUsername(), tempUser.getPassword()));
            } else {
                throw new UnauthorizedException();
            }
        }else {
            throw new ForbiddenException();
        }
    }
}
